import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)


const store = new Vuex.Store({
	//仓库中共有属性的集合，用于存储公共状态，只存储
	state: {
		hasLogin: false,//公共属性
		userInfo: {} ,//公共属性
		token:'',
		
	},
	//vuex中mutations是专门用来修改state中属性状态的方法集合（对象）
	mutations: {
		login(state, provider) { //方法名随便起，参数state是固定写法
			state.hasLogin = true;
			state.userInfo = provider;
			state.token = provider.token;
			//store 存储json数据格式的用户信息
			uni.setStorageSync('userInfo', provider);
			uni.setStorageSync('thoruitoken_token', provider.token)
			
			if(provider.roleList != null && provider.roleList != undefined && provider.roleList.length > 0){
				uni.setStorageSync('roleList', provider.roleList);
			}
		},
		logout(state) {
			state.hasLogin = false;
			state.userInfo = {};
			uni.removeStorageSync('userInfo');
			uni.removeStorageSync('token');
		}
	},
	actions: {
	
	},
	getters: {
		getUserInfo(state) {
			return state.userInfo
		}
	}
})

export default store

